package Servlets;

import Beans.UserManagerBean;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "UserManagerServlet", urlPatterns = {"/UserManagerServlet"})
public class UserManagerServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            InputStream inputStream = request.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            String operation = (String) objectInputStream.readObject();
            
            if (operation.equals("login")){
                String username = (String) objectInputStream.readObject();
                String password = (String) objectInputStream.readObject();
                
                HttpSession httpSession = request.getSession(true);
                UserManagerBean userManagerBean = (UserManagerBean) httpSession.getAttribute("UserBean");
                
                if (userManagerBean == null) {
                    InitialContext context = new InitialContext();
                    userManagerBean = (UserManagerBean) context.lookup("java:module/UserManagerBean");
                    httpSession.setAttribute("UserBean", userManagerBean);
                }
                
                boolean loged = userManagerBean.logIn(username, password);
                OutputStream outputStream = response.getOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(loged);
                objectOutputStream.flush();
                objectOutputStream.close();
                
            } else if (operation.equals("register")){
                String username = (String) objectInputStream.readObject();
                String password = (String) objectInputStream.readObject();
                String name = (String) objectInputStream.readObject();
                String surname = (String) objectInputStream.readObject();
                String email = (String) objectInputStream.readObject();
                objectInputStream.close();
                
                HttpSession httpSession = request.getSession(true);
                UserManagerBean userManagerBean = (UserManagerBean) httpSession.getAttribute("UserBean");
                
                if (userManagerBean == null) {
                    InitialContext context = new InitialContext();
                    userManagerBean = (UserManagerBean) context.lookup("java:module/UserManagerBean");
                    httpSession.setAttribute("UserBean", userManagerBean);
                }
                
                boolean registered = userManagerBean.register(username, password, name, surname, email);
                OutputStream outputStream = response.getOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(registered);
                objectOutputStream.flush();
                objectOutputStream.close();
            }
        } catch (Exception ex) {
            File file = new File("C:/UserManagerServlet_processRequest.txt");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(ex.getMessage());
            objectOutputStream.flush();
            objectOutputStream.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
